var $content = $('<div class="popup-content"></div>');


function build_container() {
	if($('.popup-close').length == 0)
		$('body').append(
			$('<div class="popup-close"></div>')
			.append($content)
			.click(function(event) {
				$(this).css({
					display: 'none'
				});
			})
		);
};


function popup(config){
	build_container();
	$content.empty();
	(config || []).forEach(function(g){
		if(g.view && g.handler && typeof g.handler === 'function'){
			$content.append(
				$('<div class="popup-item">'+g.view+'</div>')
					.click(function(event) {
						/* Act on the event */
						g.handler();
						ret.close();
					})
				);
		}
	});
	//build end
	var ret = {
		pop : function(x, y){

			var window_width = window.innerWidth;
			var window_height = window.innerHeight;

			$content.css({display:"block"});
			$('.popup-close').css({display:"block"});
			if (x >= window_width - 200) {
				$content.css({
					display:'block',
					left: x - 200,
					top: y
				});
			}else{
				$content.css({
					display:'block',
					left: x,
					top: y
				});
			};
			//this.close();
		},
		close: function(){
			$content.css({display:"none"});
		}
	}
	return ret;
};	
module.exports = popup;	